package com.otod.sys.menu.service.impl;

import com.otod.base.exception.AppException;
import com.otod.base.service.BaseService;
import com.otod.commons.utils.Collections3;
import com.otod.commons.utils.StringUtils;
import com.otod.sys.menu.bo.MenuTreeBO;
import com.otod.sys.menu.mapper.MenuMapper;
import com.otod.sys.menu.service.IMenuService;
import com.otod.sys.vo.AuapTMenuVo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class MenuServiceImpl extends BaseService implements IMenuService{
	protected static final Logger logger = LoggerFactory.getLogger(MenuServiceImpl.class);
	
	@Autowired  
	private MenuMapper menuMapper;
	
	/**
	 * 获取相关用户所有菜单树---分层次
	 * @return
	 */
	public List<MenuTreeBO> getMenuTree(String empNO){
		Map<String, Object> params = new HashMap<String,Object>();
		params.put("empNO", empNO);
		List<AuapTMenuVo> list = queryForList(params);
		return this.formatMenuTree(list);
	}
	
	/**
	 * 获取所有菜单树List
	 * @return
	 */
	public List<AuapTMenuVo> getAllMenuTreeList(){
		List<AuapTMenuVo> list = queryForList(new HashMap<String,Object>());
		return list;
	}
	
	/**
	 * 获取所有类型为菜单的menuList
	 * @return
	 */
	public List<AuapTMenuVo> getAllMenuList(){
		List<AuapTMenuVo> list = queryForList(new HashMap<String,Object>());
		List<AuapTMenuVo> list1 = new ArrayList<AuapTMenuVo>();
		for (int i = 0; i < list.size(); i++) {
			if(Integer.parseInt(list.get(i).getMenuType()) == 1){
				list1.add(list.get(i));
			}
		}
		return list1;
	}
	
	/**
	 * 查询菜单列表
	 */
	public List<AuapTMenuVo> queryForList(Map<String, Object> params){
		return menuMapper.queryForList(params);
	}
	
	/**
	 * 格式化为树的数据
	 * @param voList
	 * @return
	 */
	private List<MenuTreeBO> formatMenuTree(List<AuapTMenuVo> voList){
		List<MenuTreeBO> formatList = new ArrayList<MenuTreeBO>();
		for (AuapTMenuVo menuVO : voList) {
			//如果为父节点
			if(StringUtils.isEmpty(menuVO.getParentMenuId())){
				MenuTreeBO menuBO = menuVO2TreeBO(menuVO);
				formatList.add(menuBO);
				
				this.setChildMenu(voList, menuBO);
			}
		}
		Collections.sort(formatList);//排序
		
		return formatList;
	}
	
	/**
	 * 添加下级树
	 * @param voList
	 * @param menuBO
	 */
	private void setChildMenu(List<AuapTMenuVo> voList, MenuTreeBO menuBO){
		List<MenuTreeBO> childMenu = new ArrayList<MenuTreeBO>();
		List<AuapTMenuVo> usedList = new ArrayList<AuapTMenuVo>();
		for (AuapTMenuVo menuVO : voList) {
			if(menuBO.getId().equals(menuVO.getParentMenuId())){
				MenuTreeBO menuBO2 = this.menuVO2TreeBO(menuVO);
				childMenu.add(menuBO2);
				usedList.add(menuVO);
				menuBO.setHasChild(true);
				
				this.setChildMenu(voList, menuBO2);
			}
		}
		if(childMenu.size() > 0){
			Collections.sort(childMenu);//排序
			menuBO.setData(childMenu);
			menuBO.setType("folder");
		}else{
			menuBO.setType("item");
		}
	}
	
	/**
	 * menu VO 转 tree所需VO
	 * @param menuVO
	 * @return
	 */
	private MenuTreeBO menuVO2TreeBO(AuapTMenuVo menuVO) {
		String url = menuVO.getMenuUri();
		
		MenuTreeBO menuTreeBO = new MenuTreeBO();
		menuTreeBO.setId(menuVO.getUuid());
		menuTreeBO.setName(menuVO.getMenuName());
		menuTreeBO.setUrl(url);
		menuTreeBO.setParentId(menuVO.getParentMenuId());
		menuTreeBO.setMenuOrder(menuVO.getMenuOrder());
		return menuTreeBO;
	}
	
	
	/**
	 * 通过菜单编号查询菜单详细信息
	 * @return
	 */
	public AuapTMenuVo getMenuById(String menuId){
		return menuMapper.getById(menuId);
	}
	
	
	/**
	 * 新增
	 */
	@Transactional(readOnly = false)
	public void insert(AuapTMenuVo auapTMenu){
		auapTMenu.setUuid(super.getUUID());
		menuMapper.insert(auapTMenu);
	}
	
	/**
	 * 修改
	 */
	@Transactional(readOnly = false)
	public void update(Map map){
		menuMapper.update(map);
	}
	
	/**
	 * 删除
	 * @throws AppException 
	 */
	@Transactional
	public void deleteById(String menuId) throws AppException{
		List<AuapTMenuVo> childrenList = queryChildrenForList(menuId);
		if(Collections3.isNotEmpty(childrenList)){
			throw new AppException("只能删除无子节点的菜单！");
		}
		
		menuMapper.deleteById(menuId);
	}
	
	/**
	 * 获取菜单的子孙节点
	 * @param pMenuId 父级菜单ID
	 * @return
	 */
	public List<AuapTMenuVo> queryChildrenForList(String pMenuId){
		List<AuapTMenuVo> childrenList = new ArrayList<AuapTMenuVo>();
		List<AuapTMenuVo> allList = getAllMenuTreeList();
		if(pMenuId != null){
			
			getChildrenMenu(pMenuId, childrenList, allList);
			return childrenList;
		}
		
		return allList;
	}
	
	/**
	 * 子节点菜单递归
	 * @param pMenuId
	 * @param childrenList
	 * @param allList
	 */
	private void getChildrenMenu(String pMenuId,List<AuapTMenuVo> childrenList,List<AuapTMenuVo> allList){
		for (AuapTMenuVo auapTMenuVo : allList) {
			if(pMenuId.equals(auapTMenuVo.getParentMenuId())){
				childrenList.add(auapTMenuVo);
				getChildrenMenu(auapTMenuVo.getUuid(), childrenList, allList);
			}
		}
	}
}
